IF OBJECT_ID(N'SendGoodmanCard', N'P') IS NULL
BEGIN
    EXEC (
             'CREATE PROCEDURE [dbo].[SendGoodmanCard] AS BEGIN SELECT 1 END'
         )
END
GO

ALTER PROCEDURE SendGoodmanCard
	@UserId INT
AS
BEGIN
	-- Only a diaos can recive a goodman card
	DECLARE @DiaosId INT
	SELECT @DiaosId = ut.Id
	FROM   dbo.UserType ut
	WHERE  ut.Name = 'Diaos'
	
	DECLARE @Temp INT
	SELECT @Temp = COUNT(uc.UserId)
	FROM   dbo.UserCard uc
	WHERE  uc.UserId = @UserId 
	
	IF (@Temp > 0)
	    -- If a diaos already has goodman card
	    UPDATE dbo.UserCard
	    SET    CardNumber  = CardNumber + 1
	    WHERE  UserId      = @UserId
	ELSE
	    -- If this is a new diaos
	    INSERT INTO dbo.UserCard
	      (
	        UserId,
	        CardNumber
	      )
	    VALUES
	      (
	        @UserId,
	        1
	      )
END
GO